package encoder

import (
	"gitee.com/general252/gomedia/go-codec"
	"github.com/bluenviron/gortsplib/v4/pkg/format/rtpmpeg4audio"
	"github.com/pion/rtp"
)

type AACDecoder2 struct {
	rtpmpeg4audio.Decoder

	Asc []byte
}

func (d *AACDecoder2) Init() error {
	return d.Init()
}

func (d *AACDecoder2) Decode(pkt *rtp.Packet) ([][]byte, error) {
	payload := pkt.Payload
	if payload[0] != 0x00 || payload[1] != 0x10 {
		var frame []byte
		// 没有 AU-headers
		if payload[0] == 0xFF && payload[1] == 0xF1 {
			// adts
			frame = payload
		} else {
			var adts []byte
			if len(d.Asc) > 0 {

				adtsHdr, _ := codec.ConvertASCToADTS(d.Asc, len(payload)+7)
				adts = adtsHdr.Encode()
			}
			frame = append(adts, payload...)
		}
		return [][]byte{frame}, nil
	}

	return d.Decoder.Decode(pkt)
}
